/**
  *@File文件:CardService.java
  *@Date日期:2017年7月14日 下午2:37:15
  *@Copyright版权:Corporation 2017
  */
package com.qiaomengapp.serviceImp;

import java.util.Date;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qiaomengapp.bean.Card;
import com.qiaomengapp.bean.UserRecord;
import com.qiaomengapp.dao.CardMapper;
import com.qiaomengapp.dao.UserRecordMapper;
import com.qiaomengapp.exception.CustomException;
import com.qiaomengapp.service.ICardService;

/**
 *@Description描述:
 *@Author作者 jinxin
 *@Date日期:2017年7月14日
 */
@Service
public class ImpCardService implements ICardService{
	@Autowired
	private CardMapper cardMapper;
	@Autowired
	private UserRecordMapper userRecordMapper;
	//绑定银行卡
	public String selectCheckCard(long cardCode,int userId) throws CustomException{
		Card card = cardMapper.selectCardBindUser(cardCode, userId);
		if(card!=null){
			throw new CustomException("该用户已绑定过该银行卡");	
		}
		int count = cardMapper.selectCountCard(cardCode);
		if(count>=3){
			throw new CustomException("该银行卡已被3位用户或3位以上用户绑定");	
		}
		return "绑定银行卡成功";		
	}
	//充钱,提现
	public int update_chargeMoney(long cardCode,int userId,double money) throws CustomException{
		Card card = cardMapper.selectCardBindUser(cardCode, userId);
		int i = cardMapper.update_chargeMoney(card, money);
		if(i==0){
			throw new CustomException("充值或提现失败");	
		}
		int j = userRecordMapper.insert_userRecord(new UserRecord(null, userId, money, 1, new Date()));
		if(j==0){
			throw new CustomException("用户资金变动记录失败");	
		}
		return j;		
	}
	//付钱,取钱
	public int update_payMoney(long cardCode,int userId,double money) throws CustomException{
		Card card = cardMapper.selectCardBindUser(cardCode, userId);
		int i = cardMapper.update_payMoney(card, money);
		if(i==0){
			throw new CustomException("付钱或取钱失败,可能余额不足");	
		}
		int j = userRecordMapper.insert_userRecord(new UserRecord(null, userId, money, 2, new Date()));
		if(j==0){
			throw new CustomException("用户资金变动记录失败");	
		}
		return j;
	}
	
}
